package com.wangsd.web.common.controller;

import com.wangsd.common.basicbean.Result;
import com.wangsd.web.common.entity.UploadFile;
import com.wangsd.web.common.service.UploadFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传控制器
 */

@RestController
@RequestMapping("/common/upload")
@Api(value = "upload", tags = "文件通用异步上传")
@Slf4j
public class UploadController {

    @Autowired
    UploadFileService uploadFileService;

    /**
     * 上传图片
     * @param file
     * @return
     */
    @ApiOperation(value = "上传图片", notes = "上传图片", httpMethod = "POST")
    @RequestMapping(value = "uploadImg", method = RequestMethod.POST)
    public Object uploadImg(@ApiParam(value = "图片文件", required = true) @PathVariable("file") MultipartFile file) {
        return uploadFileService.uploadImg(file);
    }

    /**
     * 上传文件
     * @param file
     * @return
     */
    @ApiOperation(value = "上传文件", notes = "上传文件", httpMethod = "POST")
    @RequestMapping(value = "uploadFile", method = RequestMethod.POST)
    public Object uploadFile(@ApiParam(value = "文件", required = true) @PathVariable("file") MultipartFile file) {
        return uploadFileService.uploadFile(file);
    }

    /**
     * 上传证书
     * @param file
     * @return
     */
    @ApiOperation(value = "上传证书", notes = "上传证书", httpMethod = "POST")
    @RequestMapping(value = "uploadCertificateFile", method = RequestMethod.POST)
    public Object uploadCertificateFile(@ApiParam(value = "文件", required = true) @PathVariable("file") MultipartFile file) {
        return uploadFileService.uploadCertificateFile(file);
    }

    /**
     * 删除图片
     * @param name
     * @return
     */
    @ApiOperation(value = "删除图片", notes = "删除图片", httpMethod = "GET")
    @RequestMapping(value = "/deletePic", method = RequestMethod.GET)
    public Result deletePic(@ApiParam(value = "文件名", required = true) @RequestParam("name") String name) {
        uploadFileService.deleteImg(name);
        return Result.success();
    }

    /**
     * 删除文件
     * @param name
     * @return
     */
    @ApiOperation(value = "删除文件", notes = "删除文件", httpMethod = "GET")
    @RequestMapping(value = "/deleteFile", method = RequestMethod.GET)
    public Result deleteFile(@ApiParam(value = "文件名", required = true) @RequestParam("name") String name) {
        uploadFileService.deleteFile(name);
        return Result.success();
    }

    /**
     * 上传apk或者ipa
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "上传apk文件", notes = "上传apk文件", httpMethod = "POST")
    @RequestMapping("/uploadFileApk")
    public Object uploadFile(MultipartFile file,String versionNo) {
        UploadFile uploadFile = uploadFileService.uploadFile(file, versionNo);
        return Result.success(uploadFile);
    }
}
